<template>
  <div>
      <van-nav-bar  title="黑马头条登录" />
      <van-form @submit="onSubmit">
  <van-field
    v-model="user.mobile"
    required
    name="mobile"
    label="手机号"
    placeholder="请输入11位手机号"
    :rules="[{ required: true, message: '请填正确写手机号',pattern:/^1[3-9]\d{9}$/ }]"
  />
  <van-field
    v-model="user.code"
    type="password"
    required
    name="code"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写6位密码' ,pattern:/^\d{6}$/}]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit"
    :loading="isLoading"
    :disabled="isLoading">提交</van-button>
  </div>
</van-form>

  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { Notify } from 'vant'
import { setToken } from '@/utils/token.js'
export default {
  data () {
    return {
      user: {
        code: '246810',
        mobile: '14345678910'
      },
      isLoading: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.isLoading = true
      console.log('submit', values)
      try {
        const res = await loginAPI(values)
        console.log(res)
        setToken(res.data.data.token)
        Notify({ type: 'success', message: '登录成功' })
        localStorage.setItem('refresh_token', res.data.data.refresh_token)
        this.$router.replace({
          path: '/layout/home'
        })
      } catch (err) {
        Notify({ type: 'danger', message: '账户或密码错误' })
      }
      this.isLoading = false
    }
  }
}
</script>

<style scoped lang='less'>
// .van-nav-bar{
//     background-color:#007bff;
// }
// /deep/ .van-nav-bar__title{
// color: white;
// }
</style>
